package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.management.basics.domain.TEmployee;
import com.ruoyi.management.basics.service.ITCompanyService;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.GetDriverDetailVo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/driver/manage")
public class DriverManageController extends BaseController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITEmployeeService iTEmployeeService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ITEmployeeService employeeService;

    @Autowired
    private ITCompanyService companyService;


    /**
     * 驾驶员登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/driverLogin")
    public AjaxResult driverLogin(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.driverLogin(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 监管登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/superviseLogin")
    public AjaxResult superviseLogin(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.superviseLogin(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/addDriver")
    public AjaxResult addDriver(@RequestBody SysUser user)
    {

        //判断驾驶员信息是否已在监管端注册
        String tel = user.getPhonenumber();
        String idCard = user.getIdCard();
        String name = user.getUserName();
        //TEmployee employee = iTEmployeeService.getTEmployeeByTelAndIdCard(tel,idCard);
        TEmployee employee = iTEmployeeService.getTEmployeeByNameAndIdCard(name,idCard);
        if (null == employee)
        {
            return AjaxResult.error("注册失败，驾驶员信息未在监管端注册");
        }

        //判断驾驶员信息是否已在驾驶员端注册
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getPhonenumber())))
        {
            return AjaxResult.error("注册失败，登录账号已存在");
        }

        Long[] roleIds = new Long[1];
        roleIds[0] = Long.valueOf(100);//初始化角色信息  100 - 驾驶员
        user.setRoleIds(roleIds);
        user.setUserType("02");//驾驶员

        //初始化驾驶员部门信息（所属企业）
        SysDept dept = deptService.getDeptByDeptName(employee.getCompanyName());
        if (null!=dept)
        {
            user.setDeptId(dept.getDeptId());
        }
        user.setUserName(tel);
        user.setNickName(tel);
        user.setIdCard(idCard);
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }

    @PostMapping("/valDriver")
    public AjaxResult valDriver(@RequestBody SysUser user)
    {

        //判断驾驶员信息是否已在监管端注册
        String tel = user.getPhonenumber();
        String idCard = user.getIdCard();
        String name = user.getUserName();
        //TEmployee employee = iTEmployeeService.getTEmployeeByTelAndIdCard(tel,idCard);
        TEmployee employee = iTEmployeeService.getTEmployeeByNameAndIdCard(name,idCard);
        if (null == employee)
        {
            return AjaxResult.error("校验失败，驾驶员信息未在监管端注册");
        }

        //判断驾驶员信息是否已在驾驶员端注册
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getPhonenumber())))
        {
            return AjaxResult.error("校验失败，登录账号已存在");
        }

        //GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(tel);
        GetDriverDetailVo vo =  employeeService.getEmployeeDetailByIdcard(idCard);
        if (null !=vo && null != vo.getRcompanyid() && !"".equals(vo.getRcompanyid()))
        {
            String recompanyName = companyService.getCompanyNamesByRecompanyId(vo.getRcompanyid());
            vo.setRcompanyName(recompanyName);
        }
        return AjaxResult.success(vo);
    }
}
